﻿Imports System
Imports System.Collections.Generic
Imports System.Reflection
Imports System.Workflow.ComponentModel
Imports System.Workflow.ComponentModel.Compiler
Imports System.Workflow.ComponentModel.Design

Namespace System.Workflow.Activities
    Friend NotInheritable Class WebServiceFaultValidator
        Inherits ActivityValidator
        ' Methods
        Public Overrides Function Validate(ByVal manager As ValidationManager, ByVal obj As Object) As ValidationErrorCollection
            Dim errors As ValidationErrorCollection = MyBase.Validate(manager, obj)
            Dim activity As WebServiceFaultActivity = [TryCast](obj,WebServiceFaultActivity)
            If (activity Is Nothing) Then
                Throw New ArgumentException(SR.GetString("Error_UnexpectedArgumentType", New Object() { GetType(WebServiceFaultActivity).FullName }), "obj")
            End If
            If Not Helpers.IsActivityLocked(activity) Then
                Dim list As List([Of] ParameterInfo)
                Dim list2 As List([Of] ParameterInfo)
                Dim activity2 As WebServiceInputActivity = Nothing
                If String.IsNullOrEmpty(activity.InputActivityName) Then
                    errors.Add(ValidationError.GetNotSetValidationError("InputActivityName"))
                    Return errors
                End If
                Dim service As ITypeProvider = DirectCast(manager.GetService(GetType(ITypeProvider)), ITypeProvider)
                If (service Is Nothing) Then
                    Throw New InvalidOperationException(SR.GetString("General_MissingService", New Object() { GetType(ITypeProvider).FullName }))
                End If
                Dim flag As Boolean = False
                Dim activity3 As Activity
                For Each activity3 In WebServiceActivityHelpers.GetPreceedingActivities(activity)
                    If ((Not TypeOf activity3 Is WebServiceFaultActivity OrElse (String.Compare(DirectCast(activity3, WebServiceFaultActivity).InputActivityName, activity.InputActivityName, StringComparison.Ordinal) <> 0)) AndAlso (Not TypeOf activity3 Is WebServiceOutputActivity OrElse (String.Compare(DirectCast(activity3, WebServiceOutputActivity).InputActivityName, activity.InputActivityName, StringComparison.Ordinal) <> 0))) Then
                        [Continue] For
                    End If
                    If TypeOf activity3 Is WebServiceFaultActivity Then
                        errors.Add(New ValidationError(SR.GetString("Error_DuplicateWebServiceFaultFound", New Object() { activity3.Name, activity.InputActivityName }), &H574))
                        Return errors
                    End If
                    errors.Add(New ValidationError(SR.GetString("Error_DuplicateWebServiceResponseFound", New Object() { activity3.Name, activity.InputActivityName }), &H56A))
                    Return errors
                Next
                Dim activity4 As Activity
                For Each activity4 In WebServiceActivityHelpers.GetPreceedingActivities(activity)
                    If (String.Compare(activity4.QualifiedName, activity.InputActivityName, StringComparison.OrdinalIgnoreCase) = 0) Then
                        If TypeOf activity4 Is WebServiceInputActivity Then
                            activity2 = [TryCast](activity4,WebServiceInputActivity)
                            flag = True
                            Exit For
                        End If
                        flag = False
                        errors.Add(New ValidationError(SR.GetString("Error_WebServiceReceiveNotValid", New Object() { activity.InputActivityName }), &H564))
                        Return errors
                    End If
                Next
                If Not flag Then
                    errors.Add(New ValidationError(SR.GetString("Error_WebServiceReceiveNotFound", New Object() { activity.InputActivityName }), &H55E))
                    Return errors
                End If
                Dim interfaceType As Type = Nothing
                If (Not activity2.InterfaceType Is Nothing) Then
                    interfaceType = service.GetType(activity2.InterfaceType.AssemblyQualifiedName)
                End If
                If (interfaceType Is Nothing) Then
                    errors.Add(New ValidationError(SR.GetString("Error_WebServiceReceiveNotConfigured", New Object() { activity2.Name }), &H566))
                    Return errors
                End If
                If String.IsNullOrEmpty(activity2.MethodName) Then
                    errors.Add(New ValidationError(SR.GetString("Error_WebServiceReceiveNotConfigured", New Object() { activity2.Name }), &H566))
                    Return errors
                End If
                Dim interfaceMethod As MethodInfo = Helpers.GetInterfaceMethod(interfaceType, activity2.MethodName)
                If (interfaceMethod Is Nothing) Then
                    errors.Add(New ValidationError(SR.GetString("Error_WebServiceReceiveNotConfigured", New Object() { activity2.Name }), &H566))
                    Return errors
                End If
                WebServiceActivityHelpers.GetParameterInfo(interfaceMethod, list, list2)
                If (list2.Count = 0) Then
                    errors.Add(New ValidationError(SR.GetString("Error_WebServiceFaultNotNeeded"), &H57A))
                    Return errors
                End If
            End If
            Return errors
        End Function

    End Class
End Namespace

